<template>
  <div class="patient-records">
    <div class="records-header">
      <h2>我的病历</h2>
      <el-button type="primary" icon="Plus" @click="createNewRecord">创建新病历</el-button>
    </div>
    
    <el-card class="records-card">
      <el-table :data="medicalRecords" style="width: 100%" stripe>
        <el-table-column prop="date" label="日期" width="150" sortable />
        <el-table-column prop="hospital" label="医院" width="180" />
        <el-table-column prop="department" label="科室" width="120" />
        <el-table-column prop="doctor" label="医生" width="120" />
        <el-table-column prop="diagnosis" label="诊断结果" />
        <el-table-column prop="blockHash" label="区块链哈希" min-width="180" show-overflow-tooltip>
          <template #default="scope">
            <el-link type="primary" @click="viewBlockDetails(scope.row)">
              {{ truncateHash(scope.row.blockHash) }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" icon="View" @click="viewRecordDetails(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, jumper, sizes, total"
          :total="total"
          background
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const medicalRecords = ref([
  { id: 1, date: '2023-07-21', hospital: '市中心医院', department: '内科', doctor: '张医生', diagnosis: '上呼吸道感染', blockHash: '0x9b7b0d1e4d8f0c3a6b5d2e8f1a0c3b5d2e8f1a0c3' },
  { id: 2, date: '2023-06-25', hospital: '人民医院', department: '眼科', doctor: '李医生', diagnosis: '结膜炎', blockHash: '0x742d35cc6634c0532925a3b844bc454e4438f44e' },
  { id: 3, date: '2023-05-18', hospital: '中医院', department: '骨科', doctor: '王医生', diagnosis: '手腕扭伤', blockHash: '0x5aafe7e8d70aae3a1c1b8f8c1e1a0c3b5d2e8f1a0' },
  { id: 4, date: '2023-04-12', hospital: '儿童医院', department: '儿科', doctor: '刘医生', diagnosis: '感冒发烧', blockHash: '0x3dca034f7e8d70aae3a1c1b8f8c1e1a0c3b5d2e8' },
  { id: 5, date: '2023-03-05', hospital: '中心医院', department: '内科', doctor: '张医生', diagnosis: '高血压复查', blockHash: '0x1e57997e8d70aae3a1c1b8f8c1e1a0c3b5d2e8f1' },
])

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(5)

const truncateHash = (hash) => {
  if (!hash) return ''
  return `${hash.substring(0, 6)}...${hash.substring(hash.length - 4)}`
}

const createNewRecord = () => {
  ElMessage.info('创建新病历功能')
}

const viewRecordDetails = (record) => {
  ElMessage.info(`查看病历详情: ${record.id}`)
}

const viewBlockDetails = (record) => {
  ElMessage.info(`查看区块链详情: ${record.blockHash}`)
}
</script>

<style scoped>
.patient-records {
  padding: 20px;
}

.records-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.records-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>